package ls.impl;

import ls.dao.EvaluateDAO;
import ls.entity.Evaluate;
import Connection.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EvaluateDAOImpl extends DAOBase implements EvaluateDAO {

    private static final String EVALUATE_INSERT_SQL =
            "INSERT INTO evaluate VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ";
    @Override
    public void addEvaluate(Evaluate evaluate) {
        Connection connection = null;
        try {
            connection = getConnection();
            PreparedStatement ps = connection.prepareStatement(EVALUATE_INSERT_SQL);
            ps.setString(1,evaluate.getStudent_id());
            ps.setString(2,evaluate.getStudent_name());
            ps.setString(3,evaluate.getCourse_name());
            ps.setString(4,evaluate.getTeacher_id());
            ps.setString(5,evaluate.getNumber());
            ps.setString(6,evaluate.getStudent_major());
            ps.setString(7,evaluate.getCourse_property());
            ps.setString(8,evaluate.getCourse_student());
            ps.setString(9,evaluate.getCourse_teacher());
            ps.setString(10,evaluate.getTeach_time());
            ps.setString(11,evaluate.getStudent_work());
            ps.setString(12,evaluate.getResult());
            ps.executeUpdate();
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static final String STUDENT_SELECT_SQL = "SELECT * FROM evaluate WHERE student_id=?";
    @Override
    public Evaluate getEvaluate(String student_id) {
        Evaluate evaluate = null;
        Connection connection = null;
        try {
            connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(STUDENT_SELECT_SQL);
            preparedStatement.setString(1,student_id);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next())
            {
                evaluate = new Evaluate();
                evaluate.setStudent_id(resultSet.getString("student_id"));
                evaluate.setStudent_name(resultSet.getString("student_name"));
                evaluate.setCourse_name(resultSet.getString("course_name"));
                evaluate.setNumber(resultSet.getString("number"));
                evaluate.setStudent_major(resultSet.getString("student_major"));
                evaluate.setCourse_property(resultSet.getString("course_property"));
                evaluate.setCourse_student(resultSet.getString("course_student"));
                evaluate.setCourse_teacher(resultSet.getString("course_teacher"));
                evaluate.setTeach_time(resultSet.getString("teach_time"));
                evaluate.setStudent_work(resultSet.getString("student_work"));
                evaluate.setResult(resultSet.getString("result"));
            }
            resultSet.close();
            preparedStatement.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return evaluate;

    }

    private static final String EVALUATE_SELECT_SQL = "SELECT * FROM evaluate WHERE teacher_id=? and result=?";
    public List<Evaluate> getTeacherEvaluate(String teacher_id)
    {
        List<Evaluate> evaluateList = new ArrayList<>();
        Connection connection = null;
        try {
            connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(EVALUATE_SELECT_SQL);
            preparedStatement.setString(1,teacher_id);
            preparedStatement.setString(2,"待审核");
            ResultSet rs = preparedStatement.executeQuery();
            if(rs.next())
            {
                String student_id = rs.getString("student_id");
                String student_name = rs.getString("student_name");
                String course_name = rs.getString("course_name");
                String number = rs.getString("number");
                String student_major = rs.getString("student_major");
                String course_property = rs.getString("course_property");
                String course_student = rs.getString("course_student");
                String course_teacher = rs.getString("course_teacher");
                String teach_time = rs.getString("teach_time");
                String student_work = rs.getString("student_work");
                String result = rs.getString("result");
                Evaluate evaluate = new Evaluate(student_id,student_name,course_name,teacher_id,number,student_major,
                        course_property,course_student,course_teacher,teach_time,student_work,result);
                evaluateList.add(evaluate);
            }
            rs.close();
            preparedStatement.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return evaluateList;
    }

    private static final String EVALUATE_UPDATE_SQL = "UPDATE evaluate set result=? WHERE student_id=?";
    @Override
    public void updateEvaluate(Evaluate evaluate) {
        Connection connection = null;
        try {
            connection = getConnection();
            PreparedStatement ps = connection.prepareStatement(EVALUATE_UPDATE_SQL);
            ps.setString(1,evaluate.getResult());
            ps.setString(2,evaluate.getStudent_id());
            ps.executeUpdate();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static final String EVALUATE_SELECT_SQL2 = "SELECT * FROM evaluate WHERE teacher_id=? and result!=?";
    @Override
    public List<Evaluate> getMyAllEvaluate(String teacher_id) {
        List<Evaluate> evaluateList = new ArrayList<>();
        Connection connection = null;
        try {
            connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(EVALUATE_SELECT_SQL2);
            preparedStatement.setString(1,teacher_id);
            preparedStatement.setString(2,"待审核");
            ResultSet rs = preparedStatement.executeQuery();
            if(rs.next())
            {
                String student_id = rs.getString("student_id");
                String student_name = rs.getString("student_name");
                String course_name = rs.getString("course_name");
                String number = rs.getString("number");
                String student_major = rs.getString("student_major");
                String course_property = rs.getString("course_property");
                String course_student = rs.getString("course_student");
                String course_teacher = rs.getString("course_teacher");
                String teach_time = rs.getString("teach_time");
                String student_work = rs.getString("student_work");
                String result = rs.getString("result");
                Evaluate evaluate = new Evaluate(student_id,student_name,course_name,teacher_id,number,student_major,
                        course_property,course_student,course_teacher,teach_time,student_work,result);
                evaluateList.add(evaluate);
            }
            rs.close();
            preparedStatement.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            try {
                connection.close();
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return evaluateList;
    }
}
